# This class is used in the msg pools to handle data transactions smoothly.
class Message(object):
    def __init__(self, arg):
        
        self.prefix = None
        self.message = None
        self.target_alias = None
        self.sender = None
        self.reply = None
        self.digested = False
        
        if('prefix' in arg):
            self.prefix = arg['prefix']
        if('cmd' in arg):
            self.message = arg['message']
        if('target_alias' in arg):
            self.target = arg['target_alias']
        if('sender' in arg):
            self.sender = arg['sender']

    def _get_prefix(self):
        if(self.prefix is not None):
            return self.prefix
        else:
            return ""

    def _set_prefix(self, prefix):
        self.prefix = prefix

    def _get_message(self):
        if(self.message is not None):
            return self.message
        else:
            return ""

    def _set_message(self, message):
        self.message = message

    def _get_target(self):
        if(self.target_alias is not None):
            return self.target_alias
        else:
            return ""

    def _set_target(self, target_alias):
        self.target_alias = target_alias

    def _get_name(self):
            return getName(self.target_alias)

    def _get_sender(self):
        return self.sender

    def _set_sender(self, sender):
         self.sender = sender

    def _get_reply(self):
        return self.reply

    def _set_reply(self, reply):
        self.digested = True
        self.reply = reply

    def _get_digested(self):
        return self.digested

    Prefix = property(_get_prefix, _set_prefix)
    Message = property(_get_message, _set_message)
    TargetAlias = property(_get_target, _set_target)
    Sender = property(_get_sender, _set_sender)
    TargetName = property(_get_name)
    Reply = property(_get_reply, _set_reply)
    Digested = property(_get_digested, _set_digested)

    @staticmethod
    def NewMessage(message, socket):
        # There are three cases for messages:
        #   <msg> -- usually server commands, e.g. STOP, SRVRSTAT, LIST, KILL . . .
        #   <msg:target> -- usually server functions, e.g. <HELLO:client>, <KILL:client> . . .
        #   <prfx:msg:target> -- usually lightmaster commands.
        #
        data = process(data)
        if(len(data) == 1):
            return Message({'isCmd':False, 'sender':socket, 'cmdString':data[0]})
        elif(len(data) == 2):
            return Message({'isCmd':False, 'sender':socket, 'target':data[0], 'cmdString':data[1]})
        elif(len(data) == 3):
            return Message({'isCmd':True, 'prefix':data[0], 'sender':socket, 'target':data[1], 'cmdString':data[2]})
        else:
            return None